package com.dff.jrfw;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.dff.jrfw.databinding.ActivitySetNewLoginPsdBinding;
import com.dff.jrfw.remote.CustomCallBack;
import com.dff.jrfw.remote.RetrofitUtils;
import com.dff.jrfw.requestBody.PsdUpdateDto;
import com.dff.jrfw.utils.InputStrCheckUtil;
import com.dff.jrfw.utils.ToastUtil;
import com.dff.jrfw.utils.Util;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class SetNewLoginPsdActivity extends BaseActivity implements View.OnClickListener {

    private ActivitySetNewLoginPsdBinding binding;
    private int psdVisible = 0;    //默认密码不可见
    private String account;

    public static void callMe(Context context, String account) {
        Intent intent = new Intent(context, SetNewLoginPsdActivity.class);
        intent.putExtra("account", account);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_set_new_login_psd);
        account = getIntent().getStringExtra("account");
        initView();
    }

    private void initView() {
        binding.btnSure.setOnClickListener(this);
        binding.ivEye.setOnClickListener(this);
        binding.ivTip.setOnClickListener(this);
        binding.ivInputClearPsd.setOnClickListener(this);
        binding.ivInputClearPsdAgain.setOnClickListener(this);
        binding.etNewPsd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean isFocus) {
                if (isFocus) {
                    binding.tvError.setVisibility(View.INVISIBLE);
                    binding.viewEtNewPsdBottomLine.setBackgroundColor(getResources().getColor(R.color.g999999));
                }
            }
        });
        binding.etNewPsdAgain.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean isFocus) {
                if (isFocus) {
                    binding.tvErrorAgain.setVisibility(View.INVISIBLE);
                    binding.viewEtNewPsdAgainBottomLine.setBackgroundColor(getResources().getColor(R.color.g999999));
                }
            }
        });
        binding.etNewPsd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String etNewPsd = charSequence.toString().trim();
                if (etNewPsd.length() == 0) {
                    binding.tvError.setVisibility(View.INVISIBLE);
                    binding.viewEtNewPsdBottomLine.setBackgroundColor(getResources().getColor(R.color.g999999));
                    binding.ivInputClearPsd.setVisibility(View.INVISIBLE);
                } else {
                    binding.ivInputClearPsd.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        binding.etNewPsdAgain.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String etNewPsdAgain = charSequence.toString().trim();
                if (etNewPsdAgain.length() == 0) {
                    binding.tvErrorAgain.setVisibility(View.INVISIBLE);
                    binding.viewEtNewPsdAgainBottomLine.setBackgroundColor(getResources().getColor(R.color.g999999));
                    binding.ivInputClearPsdAgain.setVisibility(View.INVISIBLE);
                } else {
                    binding.ivInputClearPsdAgain.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_tip:
                if (binding.tvTip.getVisibility() == View.INVISIBLE) {
                    binding.tvTip.setVisibility(View.VISIBLE);
                } else {
                    binding.tvTip.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.btn_sure:
                setNewPsdReq();
                Util.hideKeyBoard(binding.btnSure);
                break;
            case R.id.iv_eye:
                if (psdVisible == 0) {
                    psdVisible = 1;
                    binding.ivEye.setImageResource(R.drawable.open_eye);
                    binding.etNewPsd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    binding.etNewPsdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    psdVisible = 0;
                    binding.ivEye.setImageResource(R.drawable.cl_eye);
                    binding.etNewPsd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    binding.etNewPsdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.iv_input_clear_psd:
                binding.etNewPsd.setText("");
                break;
            case R.id.iv_input_clear_psd_again:
                binding.etNewPsdAgain.setText("");
                break;
        }
    }


    private void setNewPsdReq() {
        String etNewPsd = binding.etNewPsd.getText().toString().trim();
        String etNewPsdAgain = binding.etNewPsdAgain.getText().toString().trim();

        if (!InputStrCheckUtil.isRightLoginPsd(etNewPsd)) {
            binding.tvError.setVisibility(View.VISIBLE);
            binding.viewEtNewPsdBottomLine.setBackgroundColor(getResources().getColor(R.color.red));
            return;
        }

        if (!etNewPsd.equals(etNewPsdAgain)) {
            binding.tvErrorAgain.setVisibility(View.VISIBLE);
            binding.viewEtNewPsdAgainBottomLine.setBackgroundColor(getResources().getColor(R.color.red));
            return;
        }

        showLoading();
        PsdUpdateDto psdUpdateDto = new PsdUpdateDto();
        psdUpdateDto.setNewPassword(etNewPsd);
        psdUpdateDto.setUsername(account);
        RetrofitUtils.getService().updatePsd("1", "1", psdUpdateDto).enqueue(new CustomCallBack<ResponseBody>() {
            @Override
            public void onFailure(Call call, Throwable t) {
                super.onFailure(call, t);
                hideLoading();
            }

            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                hideLoading();
                try {
                    if (response.body() != null) {
                        String result = response.body().string();
                        JSONObject jsonObject = new JSONObject(result);
                        int code = jsonObject.getInt("code");
                        if (code == 200) {
                            ToastUtil.toast("设置成功，请重新登录");
                            LoginActivity.callMe(SetNewLoginPsdActivity.this, account);
                        } else {
                            String msg = jsonObject.getString("msg");
                            ToastUtil.toast(msg);
                        }
                    } else {
                        ResponseBody errorResponseBody = response.errorBody();
                        if (errorResponseBody != null) {
                            ToastUtil.toast(errorResponseBody.string());
                        }
                    }
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }


}